/**************************在屏幕中间绘制集结地域*****************************/
export function drawRegion(att, id, source_polygon, _map) {
  if (!att) {
    var p1 = _map.getCoordinateFromPixel([500, 300]);
    var p2 = _map.getCoordinateFromPixel([660, 250]);
    var p3 = _map.getCoordinateFromPixel([700, 400]);
    att = [p1, p2, p3];
  }
  var style = createPolygonStyle("#800080", 2, "rgba(200, 0, 255, 0.1)");
  console.log(att);
  var arrPoints = createRegionPoints(att[0], att[1], att[2]);
  if (!id) {
    id = Math.random();
  }
  var f = createPolygon("region" + id, "region", [arrPoints], style);
  source_polygon.addFeature(f);
  return att;
}

export function clearRegion(source_polygon) {
  source_polygon.clear();
}

//鼠标绘制图形 Point 点 lineString 折线 Polygon 多边形 Cricle 圆形 Square 正方形  Box 矩形
export function addDrawOnMap(type, _map) {
  var drawonmap; // global so we can remove it later
  //The geometry type. One of 'Point', 'LineString', 'LinearRing', 'Polygon', 'MultiPoint', 'MultiLineString', 'MultiPolygon', 'GeometryCollection', 'Circle'.
  if (drawonmap) {
    _map.removeInteraction(drawonmap);
  }
  if (type !== "None") {
    var geometryFunction, maxPoints;
    if (type === "Square") {
      type = "Circle";
      geometryFunction = ol.interaction.Draw.createRegularPolygon(4);
    } else if (type === "Box") {
      type = "LineString";
      maxPoints = 2;
      geometryFunction = function (coordinates, geometry) {
        if (!geometry) {
          geometry = new ol.geom.Polygon(null);
        }
        var start = coordinates[0];
        var end = coordinates[1];
        geometry.setCoordinates([
          [start, [start[0], end[1]], end, [end[0], start[1]], start]
        ]);
        return geometry;
      };
    }

    var style = createPolygonStyle("#808080", 2, "rgba(200, 0, 255, 0.1)");
    drawonmap = new ol.interaction.Draw({
      source: source_draw,
      style: style,
      type: /** @type {ol.geom.GeometryType} */ (type),
      geometryFunction: geometryFunction,
      maxPoints: maxPoints
    });
    _map.addInteraction(drawonmap);
  }
}
export function clearDrawOnMap(source_draw) {
  source_draw.clear();
  addDrawOnMap("None");
}
